package web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import objects.Board;
import objects.Character;
import objects.Room;
import objects.Weapon;
import utils.CaseMachine;

/**
 * Servlet implementation class LoseServlet
 */
@WebServlet(name = "Finish", urlPatterns = {"/Finish"})
public class FinishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FinishServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("browserPlayer", request.getParameter("browserPlayer"));
		// if we got to page cause of victory, send the user to the victory page.
		if (request.getParameter("reason").equals("winner"))
		{
			Room room = Room.valueOf(Board.getInstance().getBdGameData().getEnvelope().getRoomCard().getName());
			Character character = Character.valueOf(Board.getInstance().getBdGameData().getEnvelope().getCharacterCard().getName());
			Weapon weapon = Weapon.valueOf(Board.getInstance().getBdGameData().getEnvelope().getWeaponCard().getName());
			
			request.setAttribute("room", CaseMachine.getImgFromRoom(room));
			request.setAttribute("character", CaseMachine.getImgFromCharacter(character));
			request.setAttribute("weapon", CaseMachine.getImgFromWeapon(weapon));
			request.setAttribute("winner", Board.getInstance().getCurrPlayerInstance().getPlayerName());
			request.setAttribute("browserPlayer", request.getParameter("browserPlayer"));
			request.setAttribute("pageName", "winner");
		}
		// else, get lost!
		else
		{
			request.setAttribute("browserPlayer", request.getParameter("browserPlayer"));
			request.setAttribute("pageName", "lost");
		}
    	// Call index.jsp (the page to call was set by pageName on the jsp)
    	RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    	dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
